package org.mikhaylov.cp.service.command.impl;

import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.mikhaylov.cp.service.command.ServiceCommand;
import org.mikhaylov.cp.service.db.entities.Inventory;
import org.mikhaylov.cp.service.db.entities.InventoryType;
import org.mikhaylov.cp.service.db.entities.Take;
import org.mikhaylov.cp.service.db.entities.User;
import org.mikhaylov.cp.service.hibernate.HibernateManager;
import org.mikhaylov.cp.service.response.ResponseType;

public class AddInventoryServiceSommand extends ServiceCommand {

	private static final long serialVersionUID = -902689434082726685L;
	private String name = null;
	private String description = null;
	private InventoryType inventoryType = null;
	private User user = null;

	@Override
	protected boolean checkParams(HttpServletRequest request) {
		if (request.getAttribute("name") == null
				|| request.getAttribute("description") == null
				|| request.getAttribute("inventoryType") == null
				|| request.getAttribute("userId") == null
				|| String.valueOf(request.getAttribute("name")).isEmpty()
				|| String.valueOf(request.getAttribute("description"))
						.isEmpty()
				|| String.valueOf(request.getAttribute("inventoryType"))
						.isEmpty()
				|| String.valueOf(request.getAttribute("userId")).isEmpty()
				|| !(request.getAttribute("name") instanceof String)
				|| !(request.getAttribute("description") instanceof String)
				|| (!(request.getAttribute("inventoryType") instanceof InventoryType) && !(request
						.getAttribute("inventoryType") instanceof Long))
				|| (!(request.getAttribute("userId") instanceof User) && !(request
						.getAttribute("userId") instanceof Long))) {
			response.setType(ResponseType.ERROR);
			response.setMeta("Wrong parameters");
			return false;
		}
		return true;
	}

	@Override
	protected boolean extractParams(HttpServletRequest request) {
		name = String.valueOf(request.getAttribute("name"));
		description = String.valueOf(request.getAttribute("description"));
		inventoryType = (request.getAttribute("inventoryType") instanceof InventoryType) ? (InventoryType) request
				.getAttribute("inventoryType") : HibernateManager.getInstance()
				.getInventoryType((Long) request.getAttribute("inventoryType"));
		user = (request.getAttribute("user") instanceof User) ? (User) request
				.getAttribute("user") : HibernateManager.getInstance().getUser(
				(Long) request.getAttribute("user"));
		return true;
	}

	@Override
	protected void process(HttpServletRequest request) {

		Inventory inventory = new Inventory();
		inventory.setName(name);
		inventory.setDescription(description);
		inventory.setInventoryType(inventoryType);
		inventory.setUser(user);
		inventory.setTakes(new TreeSet<Take>());

		HibernateManager.getInstance().addEntity(inventory);

		
		response.setType(ResponseType.SUCCESS);
		response.setMeta("You have been successfully added an inventory");

	}

}
